package com.view;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.controller.LocationManager;
import com.domain.Location;

@SuppressWarnings("serial")
public class RegisterLocationServlet extends HttpServlet{
	private LocationManager locManager = new LocationManager();
	//private UserManager userManager = new UserManager();
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req,resp);
	}

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String locationUsername = (String)req.getSession().getAttribute("UserName");
		//AppUser locationUser = userManager.searchAppUser(locationUsername);
		Location loc = new Location();
		String locationName = req.getParameter("txt_LocationName");
		if(locManager.locationAlreadyExists(locationName,locationUsername)){
			resp.sendRedirect("/register_location.jsp?alreadyExists=true");
		}else{
			double lat = Double.parseDouble(req.getParameter("txt_LocationLat"));
			double lng = Double.parseDouble(req.getParameter("txt_LocationLong"));
			double errorRadius = Double.parseDouble(req.getParameter("txt_ErrorRadius"));
			
			loc.setErrorRadius(errorRadius);
			loc.setLatitude(lat);
			loc.setLongitude(lng);
			loc.setLocationName(locationName);
			loc.setLocationUser(locationUsername);
			
			locManager.registerLocation(loc);
			
			resp.sendRedirect("/register_location.jsp?registeredLocation=true");
		}
	}

}
